#include "stdafx.h"

BulletMgr::BulletMgr()
{
}

BulletMgr::~BulletMgr()
{
	clear();
}

void BulletMgr::update(double dtime)
{
	std::vector<Bullet *>::iterator b = bullets.begin();

	while (b != bullets.end())
	{
		(*b)->update(dtime);
		if ((*b)->collisiondetect())
		{
			(*b)->blast();
			(*b)->hit();
			delete *b;
			b = bullets.erase(b);
		}
		else
		{
			b++;
		}
	}
}

Bullet * BulletMgr::create(const std::wstring & name, int vx, int vy, int x, int y, const std::wstring & sname, int lifespan, int rotatespeed)
{
	Bullet * n = nullptr;

	if (name == L"DotBullet")
	{
		n = new DotBullet(L"tankmissile", vx, vy, x, y, sname, lifespan, rotatespeed);
		bullets.push_back(n);
	}
	else if (name == L"Missile")
	{
		n = new Missile(L"missile", vx, vy, x, y, sname, lifespan, rotatespeed);
		bullets.push_back(n);
	}
	else if (name == L"EnemyBullet")
	{
		n = new EDotBullet(L"enemymissile", vx, vy, x, y, sname, lifespan, rotatespeed);
		bullets.push_back(n);
	}

	return n;
}

Bullet * BulletMgr::create(const std::wstring & name, const tagBullet & bullet, int x, int y, int vx, int vy)
{
	Bullet * n = nullptr;

	if (name == L"DotBullet")
	{
		n = new DotBullet(bullet, x, y, vx, vy);
		bullets.push_back(n);
	}
	else if (name == L"Missile")
	{
		n = new Missile(bullet, x, y, vx, vy);
		bullets.push_back(n);
	}
	else if (name == L"EnemyBullet")
	{
		n = new EDotBullet(bullet, x, y, vx, vy);
		bullets.push_back(n);
	}

	return n;
}

void BulletMgr::draw(Graphics * graphics)
{
	for (Bullet * b : bullets)
	{
		b->draw(graphics);
	}
}

BulletMgr * BulletMgr::getInstance()
{
	static BulletMgr manager;

	return &manager;
}

void BulletMgr::clear()
{
	for (Bullet * b : bullets)
	{
		delete b;
	}

	bullets.clear();
}

//void HeroBulletMgr::update(double dtime)
//{
//	static EnemyTank * collide = nullptr;
//
//	std::vector<Bullet *>::iterator b = bullets.begin();
//	while(b != bullets.end())
//	{
//		(*b)->update(dtime);
//		double x = (*b)->getX();
//		double y = (*b)->getY();
//		if (x <= -7.5 || x >= XSCREEN + 7.5 || y <= -7.5 || y >= YSCREEN + 7.5)			//out of the screen
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else if (Map::getInstance()->getWall()->collide(Point(x, y)))					//collide with a wall
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else if (Map::getInstance()->getSteel()->collide(Point(x, y)))					//collide with a steel
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else if (collide = EnemyMgr::getInstance()->collide({ (int)x,(int)y }))			//collide with an enemy
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else																			//nothing happens
//		{
//			b++;
//		}
//	}
//}
//
//HeroBulletMgr::HeroBulletMgr() :BulletMgr()
//{
//}
//
//HeroBulletMgr::~HeroBulletMgr()
//{
//}
//
//HeroBulletMgr * HeroBulletMgr::getInstance()
//{
//	static HeroBulletMgr manager;
//
//	return &manager;
//}
//
//EnemyBulletMgr::EnemyBulletMgr() :BulletMgr()
//{
//}
//
//EnemyBulletMgr::~EnemyBulletMgr()
//{
//}
//
//EnemyBulletMgr * EnemyBulletMgr::getInstance()
//{
//	static EnemyBulletMgr manager;
//
//	return &manager;
//}
//
//void EnemyBulletMgr::update(double dtime)
//{
//	std::vector<Bullet *>::iterator b = bullets.begin();
//	while (b != bullets.end())
//	{
//		(*b)->update(dtime);
//		double x = (*b)->getX();
//		double y = (*b)->getY();
//		if (x <= -7.5 || x >= XSCREEN + 7.5 || y <= -7.5 || y >= YSCREEN + 7.5)			//out of the screen
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else if (Map::getInstance()->getWall()->collide(Point(x, y)))					//collide with a wall
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else if (Map::getInstance()->getSteel()->collide(Point(x, y)))					//collide with a steel
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else if (Game::getInstance()->getHero()->collide(Point(x, y)))					//collide with the hero
//		{
//			delete *b;
//			b = bullets.erase(b);
//		}
//		else																			//nothing happens
//		{
//			b++;
//		}
//	}
//}